#pragma once
#include <vector>
#include <unordered_map>

#include "EventLoop.h"
#include "noncopyable.h"
#include "Timestamp.h"

class Channel;

class Poller : noncopyable
{
    public:
    using ChannelList = std::vector<Channel*>;
    
    Poller(EventLoop* loop);

    virtual ~Poller();

    // activeChannels 是对某些事件感兴趣的poller
    virtual Timestamp poll(int timeoutMs,ChannelList* activeChannels) = 0;
    virtual void updateChannel(Channel* channel) = 0;
    virtual void removeChannel(Channel* channel) = 0;
    //  判断channel是否在当前的poller中
    virtual bool hasChannel(Channel* channel) const;

    //  EventLoop可以获取默认的IO复用的具体实现
    static Poller* newDefaultPoller(EventLoop* loop);
    
    protected:
    using ChannelMap = std::unordered_map<int,Channel*>;    
    ChannelMap channels_;

    private:
    EventLoop* ownerLoop_;  // 定义poller所属的事件循环
};